export class AbstractWebSocket {

    constructor(url) {
        this.url = url;
        this.isOpen = false;
        this.result = null;
        this.param = null;
    }

    start(text) {
        this.text = text;
        this.webSocket = new WebSocket(this.url);
        this.webSocket.onopen = (e) => this.onOpen(e)
        this.webSocket.onmessage = (e) => this.onMessage(e)
        this.webSocket.onclose = (e) => this.onClose(e)
        this.webSocket.onerror = (e) => this.onError(e)
    }

    stop() {
        if (this.webSocket) {
            this.webSocket.close();
            this.webSocket = void 0;
        }
    }

    clearHistory(){

    }
    getHistory(){
        return [];
    }
    send(message) {
        if (this.webSocket && this.isOpen) {
            this.webSocket.send(message);
        }
    }


    onOpen(_) {
        this.isOpen = true;
        this.send(this.text);
    }


    onMessage(_) {
    }


    onClose(_) {
        this.isOpen = false;
    }


    onError(_) {
        this.isOpen = false;
    }
}